//
//  ReasonViewController.m
//  BMProject
//
//  Created by chris on 16/1/1.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "ReasonViewController.h"
#import "NSString+Extension.h"
#import "DataModel.h"
#import "StoresViewController.h"
#import "OrderVc.h"

@class OrderDealModel;

@interface ReasonViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *reasonMsg;
@property (weak, nonatomic) IBOutlet UILabel *reasonLable;

@end

@implementation ReasonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"驳回原因";
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    self.contentView.hidden = YES;

    self.reasonMsg.delegate  = self;
    self.reasonMsg.layer.borderColor = [UIColor grayColor].CGColor;
    self.reasonMsg.layer.borderWidth = 0.5;
    int num = 50 - self.reasonMsg.text.length;
    self.reasonLable.text = [[@"您还可输入" stringByAppendingString:kIntToString(num)] stringByAppendingString:@"个字符"];

    //手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    [_submitBtn addTarget:self action:@selector(submitMsg) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark UITextFieldDelegate 限制TextField位数的代理方法 12为
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string {
    // 工单号
    if (textView == self.reasonMsg) {
        NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:string];
        if (toBeString.length > 50 && range.length!=1){ //最大50个字符
            textView.text = [toBeString substringToIndex:50];
            return NO;
        }
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    int num = 50 - textView.text.length;
    self.reasonLable.text = [[@"您还可输入" stringByAppendingString:kIntToString(num)] stringByAppendingString:@"个字符"];
}


#pragma mark - 软键盘
-(void)keyboardHide:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)submitMsg{
    if (![NSString notEmptyOrNull:_reasonMsg.text]) {
            [SVProgressHUD showErrorWithStatus:@"请输入驳回原因"];
            return;
        }
    if (_isFromeOrder) {//来自工单
        [UserServices orderAuditOpttByToken:[[AppDelegate appDelegate] getUserInfo].token
                                         id:_orderID
                                    basicId:_orderID
                                     status:@"reson"
                                      reson:[NSString ignoreEmptyStr:_reasonMsg.text]
                                 showDialog:YES
                            completionBlock:^(int result, id responseObject) {
                                if (result == API_Request_Success) {
                                    DLog(@"审核驳回原因  工单");
                                    if ([responseObject isKindOfClass:[NSArray class]]) {
                                        NSDictionary *dict = responseObject[0];
                                        NSString *flag = dict[@"flag"];
                                        if (flag.intValue == 0) {
                                            //成功!!!
                                            for (int i = 0 ;i < self.navigationController.viewControllers.count; ++i) {
                                                UIViewController *vc = self.navigationController.viewControllers[i];
                                                if ([vc isKindOfClass:[OrderVc class]]) {
                                                    [self.navigationController popToViewController:vc animated:YES];
                                                }
                                            }
                                        }
                                    }
                                }
                            }];
    } else {
        [UserServices auditResonOptBytoken:[[AppDelegate appDelegate] getUserInfo].token
                                        id:kIntToString(_orderID.intValue)
                                    status:@"pass"
                                     reson:[NSString ignoreEmptyStr:_reasonMsg.text]
                           completionBlock:^(int result, id responseObject) {
                               if (result == API_Request_Success) {
                              if ([responseObject isKindOfClass:[NSArray class]]) {
                                  NSDictionary *dict = responseObject[0];
                                  NSString *flag = dict[@"flag"];
                                  NSString *msg  = dict[@"msg"];
                                  if (flag.intValue == 0) {
                                      DLog(@"审核驳回原因 门店");
                                      [SVProgressHUD showSuccessWithStatus:msg];
                                      for (int i = 0 ;i < self.navigationController.viewControllers.count; ++i) {
                                          UIViewController *vc = self.navigationController.viewControllers[i];
                                          if ([vc isKindOfClass:[StoresViewController class]]) {
                                              [self.navigationController popToViewController:vc animated:YES];
                                          }
                                      }
                                  } else {
                                      [SVProgressHUD showErrorWithStatus:[NSString ignoreEmptyStr:msg replaceStr:@"提交失败!!"]];
                                  }
                              }
                          }
                }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
